<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="产线或产线组" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="line">
              <j-dict-select-tag placeholder="请选择产线或产线组" v-model="model.line"
                                 dictCode="mes_line_cfg,LINE,LINE" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="工位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="op">
              <op-select placeholder="请选择工站" :line="model.line" v-model="model.op"
                         @change="rwkChange1"></op-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="type">
              <a-radio-group v-model:value="model.type" button-style="solid" @change="rwkChange(model.type)">
                <a-radio-button value="1">返工</a-radio-button>
                <a-radio-button value="3">报废</a-radio-button>
                <a-radio-button value="4">冻结</a-radio-button>
                <a-radio-button value="8">标记为OK</a-radio-button>
                <a-radio-button value="9">标记为NG</a-radio-button>
              </a-radio-group>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="返工至工位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="rwktoOp">
              <op-select placeholder="请选择返工工位" :line="model.line" v-model="model.rwktoOp"
                         @change="rwkChange3"></op-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="代码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="code">
              <a-input v-model="model.code" placeholder="请输入代码"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import OpSelect from '@comp/mes/OpSelect.vue'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'MesRwkCodeCfgForm',
  components: { OpSelect },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      isEdit: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        line: [
          { required: true, message: '请输入产线或产线组!' }
        ],
        code: [
          { required: true, message: '请输入代码!' }
        ]
      },
      url: {
        add: '/qis/mesRwkCodeCfg/add',
        edit: '/qis/mesRwkCodeCfg/edit',
        queryById: '/qis/mesRwkCodeCfg/queryById'
      },
      op: '',
      rwkOp: ''
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    rwkChange1(val) {
      this.op = val
      this.rwkChange()
    },
    rwkChange3(val) {
      this.rwkOp = val
      this.rwkChange()
    },
    rwkChange() {
      let op = this.op
      let rwkOp = ' '
      if (this.model.type === '3') {
        rwkOp = 'SCRAP'
      } else if (this.model.type === '8') {
        rwkOp = 'OK'
      } else if (this.model.type === '9') {
        rwkOp = 'NG'
      } else if (this.model.type === '4') {
        rwkOp = 'OnHold'
      } else {
        rwkOp = this.rwkOp
      }
      this.model.code = op + '->' + rwkOp
    },
    add() {
      this.isEdit = false
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      if (JSON.stringify(record) !== '{}') {
        this.isEdit = true
      }
      this.visible = true
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.isEdit) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
          })
        }

      })
    }
  }
}
</script>